﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Report
{
    public partial class RP005 : ReportForm
    {
        public RP005()
        {
            InitializeComponent();
        }

        private void fld_btnApply_Click(object sender, EventArgs e)
        {
            InitializeDataChartControl(GetValueByText(fld_lblAxisX.Text), GetValueByText(fld_lblAxisY.Text));
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RP005_Load(object sender, EventArgs e)
        {
            fld_dteSearchFromARInvoiceDate.DateTime = THSApp.WorkingDate;
            fld_dteSearchToARInvoiceDate.DateTime = THSApp.WorkingDate;
            //fld_lblBRBranchName.Text = ((BRBranchsInfo)new BRBranchsController().GetObjectByID(THSApp.CurrentCompanyInfo.FK_BRBranchID)).BRBranchName;
            InitializeDataChartControl(GetValueByText(fld_lblAxisX.Text), GetValueByText(fld_lblAxisY.Text));
        }

        public void InitializeDataChartControl(String strDataAxisX, String strDataAxisY)
        {
            String strGroupBy = String.Empty;
            if (strDataAxisX.Contains("Department"))
                strGroupBy = "ii.FK_ICDepartmentID";
             if (strDataAxisX.Contains("Category"))
                strGroupBy = "ii.FK_ICProductGroupID";
            else
                strGroupBy = "s.APSupplierID";
            strGroupBy += ",ii.ARInvoiceItemProductUnitPrice";
            DateTime fromDate = Convert.ToDateTime(fld_dteSearchFromARInvoiceDate.EditValue);
            DateTime toDate = Convert.ToDateTime(fld_dteSearchToARInvoiceDate.EditValue);
            String strQuery = ((ReportModule)Module).GenerateInvoiceItemSearchQuery(fromDate, toDate, 0, 0, 0, THSApp.CurrentCompanyInfo.FK_BRBranchID, strGroupBy);
            DataSet dsInvoiceItems = new ARInvoiceItemsController().GetDataSet(strQuery);
            THSList<ARInvoiceItemsInfo> lstARInvoiceItems = new THSList<ARInvoiceItemsInfo>();
            fld_cctrlARRP005InvoiceItems.Series[0].Points.Clear();
            if (dsInvoiceItems != null)
            {
                ICDepartmentsController objDepartmentsController = new ICDepartmentsController();
                ICProductGroupsController objProductGroupsController = new ICProductGroupsController();
                APSuppliersController objSuppliersController = new APSuppliersController();
                foreach (DataRow row in dsInvoiceItems.Tables[0].Rows)
                {
                    ARInvoiceItemsInfo objARInvoiceItemsInfo = (ARInvoiceItemsInfo)new ARInvoiceItemsController().GetObjectFromDataRow(row);
                    if (objARInvoiceItemsInfo != null)
                    {
                        String strArgumemtName = String.Empty;
                        if (strGroupBy.Contains("Department"))
                            strArgumemtName = objDepartmentsController.GetObjectNameByID(objARInvoiceItemsInfo.FK_ICDepartmentID);
                         if (strGroupBy.Contains("ProductGroup"))
                            strArgumemtName = objProductGroupsController.GetObjectNameByID(objARInvoiceItemsInfo.FK_ICProductGroupID);
                        else
                            strArgumemtName = objSuppliersController.GetObjectNameByID(Convert.ToInt32(row["APSupplierID"]));
                        if (!String.IsNullOrEmpty(strArgumemtName))
                        {
                            if (strDataAxisY.Contains("Cost"))
                                fld_cctrlARRP005InvoiceItems.Series[0].Points.Add(new SeriesPoint(strArgumemtName, new double[] { Convert.ToDouble(objARInvoiceItemsInfo.ARInvoiceItemTotalCost) }));
                            else if (strDataAxisY.Contains("Qty"))
                                fld_cctrlARRP005InvoiceItems.Series[0].Points.Add(new SeriesPoint(strArgumemtName, new double[] { Convert.ToDouble(objARInvoiceItemsInfo.ARInvoiceItemProductQty) }));
                            else
                                fld_cctrlARRP005InvoiceItems.Series[0].Points.Add(new SeriesPoint(strArgumemtName, new double[] { Convert.ToDouble(objARInvoiceItemsInfo.ARInvoiceItemNetAmount) }));
                        }

                    }

                }
            }
        }

        private void axisYToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fld_lblAxisY.Text = (sender as ToolStripMenuItem).Text;
            InitializeDataChartControl(GetValueByText(fld_lblAxisX.Text), GetValueByText((sender as ToolStripMenuItem).Text));
        }

        private void axisXToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fld_lblAxisX.Text = (sender as ToolStripMenuItem).Text;
            InitializeDataChartControl(GetValueByText(fld_lblAxisX.Text), GetValueByText(fld_lblAxisY.Text));
        }
        private string GetValueByText(String text)
        {
            string value = String.Empty;
            switch (text)
            {
                case "Nhóm thực đơn":
                    value = "Department";
                    break;
                case"Nhóm sản phẩm":
                    value = "Category";
                    break;
                case"Nhà cung cấp":
                    value = "Supplier";
                    break;
                case "Thành tiền trước thuế":
                    value = "NetTotalAmount";
                    break;
                case "Tổng giá vốn":
                    value = "TotalCostAmount";
                    break;
                case"Số lượng":
                    value = "Qty";
                    break;
                default:
                    value = String.Empty;
                    break;
            }
            return value;
        }

        private void fld_btnPrint_Click(object sender, EventArgs e)
        {
           // fld_cctrlARRP005InvoiceItems.Print(DevExpress.XtraCharts.Printing.PrintSizeMode.None);
            //this.fld_dgcARRP002InvoiceItems.PrintPriview();
            DevExpress.XtraPrinting.PrintingSystem printingSystem = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();

            try
            {
                //Set to landscape
                printingSystem.PageSettings.Landscape = true;
                this.Cursor = Cursors.WaitCursor;
                printLink.Component = this.fld_cctrlARRP005InvoiceItems;
                printLink.CreateDocument(printingSystem);
                printLink.VerticalContentSplitting = DevExpress.XtraPrinting.VerticalContentSplitting.Exact;
                printingSystem.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                printingSystem.Dispose();
                printLink.Dispose();
            }
        }

        private void fld_btnExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.Filter = "Microsoft Excel|*.xls";
            DialogResult dlgResult = dlgSave.ShowDialog();
            if (dlgResult == DialogResult.OK)
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                // this.fld_dgcARRP002InvoiceItems.ExportToXls(dlgSave.FileName);
                this.FindForm().Refresh();
               this.fld_cctrlARRP005InvoiceItems.ExportToXls(dlgSave.FileName);
                Cursor.Current = currentCursor;
                if (DialogResult.Yes == MessageBox.Show("Xuất file Excel thành công! Bạn có muốn mở file ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    System.Diagnostics.Process.Start(dlgSave.FileName);
                }
            }
        }
    }
}